﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class DragEvent_Text : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
    private static RectTransform panel_Rect;
    private Vector3 currentPointPos;
    private Vector3 onBeginPointPos;
    private static Transform _transform;


    private void Start()
    {
        panel_Rect = transform.parent.GetComponent<RectTransform>();
        //mediaPlayerUI = GameObject.FindGameObjectWithTag("MediaPlayerUI").GetComponent<Transform>();

        //自动设置按钮返回监听
        transform.parent.Find("Back_Btn").GetComponent<Button>().onClick.AddListener(EndContentText);

        _transform = transform;
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (onBeginPointPos.x < 180F)
        {
            currentPointPos = eventData.position;
            panel_Rect.localPosition = new Vector2(currentPointPos.x, 0F);
        }
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        onBeginPointPos = eventData.position;
    }

    public static void EndContentText()
    {
        AppUIManager.instance.currentPanel = AppUIManager.CurrentPanelType.MainPanel;
        _transform.Find("Viewport/Content/Text").GetComponent<Text>().text = string.Empty;
        Tweener tweener = panel_Rect.DOLocalMove(new Vector2(1600, 0), 1F);
        tweener.OnComplete(() => {
            panel_Rect.gameObject.SetActive(false);
            AppUIManager.instance.agoPanel = AppUIManager.CurrentPanelType.MainPanel;
        });
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        if (onBeginPointPos.x < 225F)
        {
            if (Vector2.Distance(onBeginPointPos, currentPointPos) > 80F)
            {
                EndContentText();
            }
            else
            {
                transform.parent.GetComponent<RectTransform>().DOLocalMove(Vector2.zero, 0.8F);
            }
        }
    }
}
